In [1]:
from io import StringIO
import pandas as pd

from bokeh.browserlib import view
from bokeh.charts import Line as LineChart
from bokeh.document import Document
from bokeh.io import output_file, save
from bokeh.models import (
    Plot, Line, ColumnDataSource, 
    LinearAxis, DatetimeAxis, DataRange1d, 
    BasicTicker
)
from bokeh.palettes import Spectral3

doc = Document()
filename = 'charts experiments.html'
output_file(filename, title='Charts', mode='relative-dev')

In [2]:
data = """
timestamp, activity, timespent
2015-06-25 06:42, ponies, NaN
2015-06-25 06:42, unicorns, NaN
2015-06-25 06:42, hard at it, NaN
2015-06-25 06:42, ponies, 0
2015-06-25 06:42, unicorns, 0
2015-06-25 06:42, hard at it, 0
2015-06-25 07:42, ponies, 0.5
2015-06-25 07:42, unicorns, 1
2015-06-25 07:42, hard at it, 0
2015-06-25 12:20, ponies, 0.9
2015-06-25 12:20, unicorns, 3
2015-06-25 12:20, hard at it, 3
2015-06-25 18:22, ponies, 1
2015-06-25 18:22, unicorns, 3
2015-06-25 18:22, hard at it, 3.2

2015-06-26 06:42, ponies, NaN
2015-06-26 06:42, unicorns, NaN
2015-06-26 06:42, hard at it, NaN
2015-06-26 06:42, ponies, 0
2015-06-26 06:42, unicorns, 0
2015-06-26 06:42, hard at it, 0
2015-06-26 07:42, ponies, 0.5
2015-06-26 07:42, unicorns, 1
2015-06-26 07:42, hard at it, 0
2015-06-26 12:20, ponies, 0.9
2015-06-26 12:20, unicorns, 3
2015-06-26 12:20, hard at it, 3
2015-06-26 18:22, ponies, 1
2015-06-26 18:22, unicorns, 3
2015-06-26 18:22, hard at it, 3.2

2015-06-27 06:42, ponies, NaN
2015-06-27 06:42, unicorns, NaN
2015-06-27 06:42, hard at it, NaN
2015-06-27 06:42, ponies, 0
2015-06-27 06:42, unicorns, 0
2015-06-27 06:42, hard at it, 0
2015-06-27 07:42, ponies, 0.5
2015-06-27 07:42, unicorns, 1
2015-06-27 07:42, hard at it, 0
2015-06-27 12:20, ponies, 0.9
2015-06-27 12:20, unicorns, 3
2015-06-27 12:20, hard at it, 3
2015-06-27 20:22, ponies, 1
2015-06-27 20:22, unicorns, 3
2015-06-27 20:22, hard at it, 3.2

2015-06-28 06:42, ponies, NaN
2015-06-28 06:42, unicorns, NaN
2015-06-28 06:42, hard at it, NaN
2015-06-28 06:42, ponies, 0
2015-06-28 06:42, unicorns, 0
2015-06-28 06:42, hard at it, 0
2015-06-28 07:42, ponies, 0.5
2015-06-28 07:42, unicorns, 1
2015-06-28 07:42, hard at it, 0
2015-06-28 12:20, ponies, 0.9
2015-06-28 12:20, unicorns, 3
2015-06-28 12:20, hard at it, 3
2015-06-28 18:22, ponies, 1
2015-06-28 18:22, unicorns, 3
2015-06-28 18:22, hard at it, 3.2

2015-06-29 06:42, ponies, NaN
2015-06-29 06:42, unicorns, NaN
2015-06-29 06:42, hard at it, NaN
2015-06-29 06:42, ponies, 0
2015-06-29 06:42, unicorns, 0
2015-06-29 06:42, hard at it, 0
2015-06-29 07:42, ponies, 0.5
2015-06-29 07:42, unicorns, 1
2015-06-29 07:42, hard at it, 0
2015-06-29 12:20, ponies, 0.9
2015-06-29 12:20, unicorns, 3
2015-06-29 12:20, hard at it, 3
2015-06-29 18:22, ponies, 1
2015-06-29 18:22, unicorns, 3
2015-06-29 18:22, hard at it, 3.2
"""
df = pd.read_csv(StringIO(data), parse_dates=True, skipinitialspace=True, index_col='timestamp')
print(df.dtypes)
df.head()


activity      object
timespent    float64
dtype: object
Out[2]:
activity timespent
timestamp
2015-06-25 06:42:00 ponies NaN
2015-06-25 06:42:00 unicorns NaN
2015-06-25 06:42:00 hard at it NaN
2015-06-25 06:42:00 ponies 0
2015-06-25 06:42:00 unicorns 0

In [3]:
ponies_df = df[df.activity == 'ponies']
unicorns_df = df[df.activity == 'unicorns']
hard_df = df[df.activity == 'hard at it']
p = ColumnDataSource(ponies_df)
u = ColumnDataSource(unicorns_df)
h = ColumnDataSource(hard_df)

In [4]:
plot = Plot(
    x_range=DataRange1d(), 
    y_range=DataRange1d(), 
    background_fill='black', 
    border_fill='black',
    toolbar_location=None,
    outline_line_color=None,
    plot_width=900,
    plot_height=300
)
ticker = BasicTicker(min_interval=1)
axis_properties = dict(
    major_label_text_color='white',
    ticker=ticker,
)
plot.add_layout(LinearAxis(**axis_properties), 'left')
plot.add_layout(DatetimeAxis(**axis_properties), 'below')
line_properties = dict(
    line_width=2,
    x='timestamp', 
    y='timespent', 
)
plot.add_glyph(p, Line(line_color=Spectral3[0], **line_properties))
plot.add_glyph(u, Line(line_color=Spectral3[1], **line_properties))
plot.add_glyph(h, Line(line_color=Spectral3[2], **line_properties))

doc.add(plot)
save(doc)
view(filename)

Do it with a chart function


In [5]:
p_time = ponies_df.timespent
p_time = p_time.dropna()
p_time.name = 'ponies'
u_time = unicorns_df.timespent
u_time = u_time.dropna()
u_time.name = 'unicorns'
h_time = hard_df.timespent
h_time = h_time.dropna()
h_time.name = 'hard at it'
chart_df = pd.DataFrame([p_time, u_time, h_time])
chart_df = chart_df.transpose()
chart_df.head()


Out[5]:
ponies unicorns hard at it
timestamp
2015-06-25 06:42:00 0.0 0 0.0
2015-06-25 07:42:00 0.5 1 0.0
2015-06-25 12:20:00 0.9 3 3.0
2015-06-25 18:22:00 1.0 3 3.2
2015-06-26 06:42:00 0.0 0 0.0

In [6]:
chart = LineChart(chart_df, palette=Spectral3)
chart.background_fill = 'black'
doc.add(chart)
save(doc)
view(filename)

Interpolation


In [7]:
ponies_only_data = """
timestamp, activity, timespent
2015-06-25 06:42, ponies, 0
2015-06-25 07:42, ponies, 0.5
2015-06-25 12:20, ponies, 0.9
2015-06-25 18:22, ponies, 1
"""
po_df = pd.read_csv(StringIO(ponies_only_data), parse_dates=True, skipinitialspace=True, index_col='timestamp')
po_df.drop('activity', 1, inplace=True)
po_df.head()


Out[7]:
timespent
timestamp
2015-06-25 06:42:00 0.0
2015-06-25 07:42:00 0.5
2015-06-25 12:20:00 0.9
2015-06-25 18:22:00 1.0

In [7]:
smoother = ponies_df.dropna().asfreq('10min').interpolate('pchip')
smoother_source = ColumnDataSource(smoother)
sm = Plot(
    x_range=DataRange1d(), 
    y_range=DataRange1d(), 
    background_fill='black', 
    border_fill='black',
    toolbar_location=None,
    outline_line_color=None,
    plot_width=900,
    plot_height=300
)
ticker = BasicTicker(min_interval=1)
axis_properties = dict(
    major_label_text_color='white',
    ticker=ticker,
)
sm.add_layout(LinearAxis(**axis_properties), 'left')
sm.add_layout(DatetimeAxis(**axis_properties), 'below')
line_properties = dict(
    line_width=2,
    x='timestamp', 
    y='timespent', 
)
sm.add_glyph(smoother_source, Line(line_color=Spectral3[0], **line_properties))
doc.add(sm)
save(doc)
view(filename)

In [ ]: